package com.woniuxy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.RbacManager;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.pageDto;
import com.woniuxy.servcie.RbacManagerservice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 佘赛飞
 * @create 2021-11-25 8:32 下午
 */
@RestController
//@RequiresAuthentication 需要登录认证
//@CrossOrigin
@RequiresRoles({"超级管理员","普通管理员"}) //需要ssf1的角色才允许登录
//@RequiresPermissions("manager::findAll")//需要manager::findAll的权限 才允许登录
@Api(value = "用户模块",tags = "RbacManagerApi")//用在类上对当前进行解释说明
public class RbacManagerController {
    @Autowired
    RbacManagerservice rbacManagerservice;
    @GetMapping("/findOne/{id}")
    @ApiOperation(value = "根据用户id查询用户信息",notes = "返回的一条用户的1所有信息")
    public RbacManager findOne(@PathVariable int id){
        return rbacManagerservice.findOne(id);
    }

    @RequestMapping("/findAll")
    @ApiOperation(value = "查询所有用户信息",notes = "返回所有用户信息，返回的数据类型是list")
    public List <RbacManager> findAll(){
        return rbacManagerservice.findAll();
    }
    //新增
    @RequestMapping("/addManager")
    @ApiOperation(value = "新增一条用户信息",notes = "新增成功返回一个int")
    @ApiImplicitParams({
         @ApiImplicitParam(name = "manager",dataType = "Object",required = true,
                 value = "封装的dto,里面放的是搜索条件")
    })
    public ResponseEntity<RbacManager> addManager(@RequestBody RbacManager manager){
        Integer i = rbacManagerservice.addManager(manager);
        if(i>0){
            return  new ResponseEntity(200,"新增成功","成功新增"+i+"条数");
        }else {
            return  new ResponseEntity(500,"新增失败","服务器在维护中");
        }
    }
    //更新
    @RequestMapping("/updateManager")
    @ApiOperation(value = "根据用户ID修改数据",notes = "修改成功返回成功")
    public ResponseEntity<RbacManager> updateManager(@RequestBody RbacManager rbacManager){
        Integer i = rbacManagerservice.Update(rbacManager);
        if(i>0){
            return  new ResponseEntity(200,"修改成功","修改新增"+i+"条数");
        }else {
            return  new ResponseEntity(500,"修改失败","服务器在维护中");
        }
    }
    //rel风格删除
    @DeleteMapping("/deleteManager/{id}")
    @ApiOperation(value = "根据用户id删除用户")
   public ResponseEntity<String>deleteManager(@PathVariable("id") int id){
       Integer i = rbacManagerservice.deleteManager(id);
       if(i>0){
           return  new ResponseEntity<>(200,"删除成功","成功删除"+i+"条数");
       }else {
           return  new ResponseEntity<>(500,"删除失败","服务器在维护中");
       }
   }
    /**
     * 分页查询所有的数据
     * @return
     */
//    @GetMapping("/FindAllAndPagination/{startPage}/{pageSize}")
//    public PageInfo<RbacManager>FindAllAndPagination(@PathVariable("startPage") int startPage
//            ,@PathVariable("pageSize") int pageSize,@RequestBody RbacManager manager){
//        PageHelper.startPage(startPage,pageSize);
//        List <RbacManager> managerList = rbacManagerservice.FindAllAndPagination(manager);
//        PageInfo<RbacManager> rbacManagerPageInfo = new PageInfo<>(managerList);
//        return rbacManagerPageInfo;
//    }
    /**
     * 分页查询所有的数据,最终版，通过ResponseEntity对返回对象再一次封装！
     * @return
     */
    @RequiresPermissions("123")//需要manager::findAll的权限 才允许登录
    @RequestMapping("/findAllResponse")
    @ApiOperation(value = "这个按条件搜索分页",notes = "返回的是一个list")
    public ResponseEntity<PageInfo<RbacManager>>findAllRes(@RequestBody pageDto pageDto){
        PageHelper.startPage(pageDto.getPage(),pageDto.getLimit());
        List <RbacManager> managerList = rbacManagerservice.FindAllAndPagination(pageDto);
        PageInfo<RbacManager> rbacManagerPageInfo = new PageInfo<>(managerList);
        return new ResponseEntity(200,"查询成功！",rbacManagerPageInfo);
    }
}
